package com.example.androidmainshixun.view;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.adapter.GoodsAdapter;
import com.example.androidmainshixun.adapter.MyBaseAdapter;
import com.example.androidmainshixun.database.DBHelper;
import com.example.androidmainshixun.entity.Goods;

import java.util.List;

public class SearchActivity extends AppCompatActivity {
    private EditText editText;
    private Button mButton;
    private DBHelper dbHelper;
    private ListView listView;
    private String[] titles={"橙子","蓝莓","香蕉","苹果","水蜜桃"};
    private String[] prices={"9.24元/斤","15.82元/斤","8.3元/斤","10.3元/斤","8.72元/斤"};
    private  String[] places={"江西省赣州市","江苏省连云港市","云南省昆明市","山东省烟台市","山西省运城市"};
    private int[] icons={R.drawable.f1,R.drawable.f2,R.drawable.f3,R.drawable.f4,R.drawable.f5};
    private List<Goods> goodsList;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        mButton=findViewById(R.id.button1);
        listView = findViewById(R.id.lv); // 获取 ListView
        EditText editText = findViewById(R.id.search_edit1);
        dbHelper = DBHelper.getInstance(this);



        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String query = editText.getText().toString();
                searchGoods(query);



            }
        });


        //初始化listview控件
        ListView listView = findViewById(R.id.lv);
        //创建一个Adapter的实例
        MyBaseAdapter mAdapter = new MyBaseAdapter(this, titles, prices, places, icons,goodsList);
        //设置Adapter
        listView.setAdapter(mAdapter);

    }

    private void searchGoods(String query) {
        List<String> goodsList = dbHelper.searchGoods(query); // 假设这个方法返回List<Goods>
        GoodsAdapter adapter = new GoodsAdapter(this, goodsList);
        listView.setAdapter(adapter); // 注意这里直接使用listView变量
    }


}
